package com.china08.yxyapi.api.manage.stu.mystudy.homework.workinfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.DateUtils;

/**
 * 根据ID请求通知作业页面所需要的信息 2017年3月9日15:12:10
 * 
 * @author lys
 *
 */

@CrossOrigin
@RestController
public class MessageWorkGetController extends YxyApiBaseController {

    @Autowired
    private HomeworkTaskQuesRepository htr;

    @Autowired
    private HomeworkTaskReportRepository htrr;
    @Autowired
    private HomeworkTaskReportQuesRepository homeworkTaskReportQuesRepository;

    /**
     * 根据任务ID请求通知作业页面所需要的信息
     * 
     * @param id
     *            作业id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/homework/workinfo/massagework", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel process(@RequestBody ReqModel reqModel, HttpServletRequest request,
            HttpServletResponse response) {
        // 判断任务ID是否为空
        Assert.notNull(StringUtils.defaultIfBlank(reqModel.homeworkTaskId, null), "通知作业的任务ID不能为空");
        // 获得数据
        RespModel respModel = new RespModel();
        // 作业
        HomeworkTaskQues homework = htr.findByTaskIdAndQuesType(reqModel.homeworkTaskId, "2").get(0);
        respModel.hT = homework;
        Assert.isTrue("2".equals(homework.getQuesType()), "该题不是通知作业类型");
        Assert.notNull(homework, "没有查到对应的作业！");

        // 获取当前用户信息
        YxyProfile yxyProfile = getProfile(request, response);
        Assert.isTrue(yxyProfile != null, "用户信息异常，请重新登录！");
        // 根据学生ID和任务id查询学生任务报告表
        HomeworkTaskReport homeworkTaskReport = htrr.findByStudentIdAndTaskId(reqModel.studentId, homework.getTaskId());
        Assert.notNull(homeworkTaskReport, "未找到当前学生的任务报告，请联系管理员");
        // 如果学生状态是未做，构建学生的答题记录，并修改作业的任务状态。
        if (homeworkTaskReport.getStatusOfFinished() == 0) {
            homeworkTaskReport.setStatusOfFinished(3);
            htrr.save(homeworkTaskReport);
        }
        respModel.predictDate = DateUtils.formatDate3(homework.getPredictDate());
        // 获取用户的答题记录
        HomeworkTaskReportQues homeworkTaskReportQues = homeworkTaskReportQuesRepository
                .findByStudentIdAndTaskIdAndQuesId(reqModel.studentId, homework.getTaskId(), homework.getQuesId());
        respModel.answerOfMyString = "";
        if (homeworkTaskReportQues != null) {
            respModel.answerOfMyString = homeworkTaskReportQues.getAnswerText();
        }
        return respModel;

    }

    // 返回的对象类型
    protected class RespModel extends YxyApiRespModel {
        // 任务截止时间的字符串格式
        public String predictDate;
        // 当前的题：
        public HomeworkTaskQues hT;
        // 当前的答题记录
        public String answerOfMyString;

    }

    // 请求的参数类型
    protected static class ReqModel extends YxyApiReqModel {
        // 获得请求的参数（任务id）
        public String homeworkTaskId;
        // 学生ID
        public String studentId;
    }
}
